<?php
/**
 * Created by PhpStorm.
 * User: hp
 * Date: 2019/4/3
 * Time: 14:47
 */

namespace App\Http\Controllers\VipChannel\Index;

use App\Modules\Channel\Models\Channel;
use App\Modules\Channel\Models\ChannelUser;
use App\Modules\OfficialAccount\Models\ForceSubscribeUsers;
use App\Modules\SendOrder\Models\SendOrder;
use App\Modules\Trade\Models\Order;
use App\Modules\Trade\Models\OrderDayStat;
use App\Modules\User\Models\User;
use App\Modules\Util;
use Illuminate\Http\Request;
use App\Http\Controllers\VipChannel\BaseController;

class IndexController extends BaseController
{
    public function getTotalInfo(Request $request)
    {
        $item = [];
        $distribution_channel_id_array = $this->getDistributionChannelIds($request);
        if (count($distribution_channel_id_array) <= 0) {
            $item['total_cost'] = 0;
            $item['today_charge_amount'] = 0;
            $item['total_charge_amount'] = 0;
            $item['yesterday_charge_amount'] = 0;
            $item['current_month_charge_amount'] = 0;
            $item['current_month_charge_amount'] = 0;
            $item['total_charge_amount_cost'] = '0%';
        } else {
            //今日
            $begin_time = date('Y-m-d');
            $item['today_charge_amount'] = number_format((float)Order::getAmount(compact('begin_time', 'distribution_channel_id_array')), 2);

            //昨日
            $begin_time = date('Y-m-d', strtotime('-1 day'));
            $end_time = $begin_time . ' 23:59:59';
            $item['yesterday_charge_amount'] = number_format(OrderDayStat::getRechargeByChannels($distribution_channel_id_array, compact('begin_time', 'end_time')), 2);

            //本月
            $begin_time = date('Y-m-01');
            $month_to_yesterday_recharge_amount = OrderDayStat::getRechargeByChannels($distribution_channel_id_array, compact('begin_time', 'end_time'));
            $item['current_month_charge_amount'] = (float)$month_to_yesterday_recharge_amount + (float)$item['today_charge_amount'];

            //累计
            $total_charge_amount_to_yesterday = OrderDayStat::getRechargeByChannels($distribution_channel_id_array, []);
            $item['total_charge_amount'] = (float)$total_charge_amount_to_yesterday + (float)$item['today_charge_amount'];

            $item['total_cost'] = SendOrder::getTotalCost($distribution_channel_id_array);
            $item['total_charge_amount_cost'] = Util::getPersentAmount($item['total_charge_amount'], $item['total_cost']);
        }
        return response()->success($item);
    }

    public function getSiteInfo(Request $request)
    {
        $channel_ids = $this->getDistributionChannelIds($request);
        if (count($channel_ids) <= 0) {
            return response()->error('NOT_EXIST');
        } else {
            $result = [];
            foreach ($channel_ids as $distribution_channel_id) {
                $item = [];
                $item['distribution_channel_id'] = $distribution_channel_id;
                $channel = Channel::getIdById($distribution_channel_id);
                $channelUser = ChannelUser::getById($channel->channel_user_id);
                $item['phone'] = $channelUser->phone;
                $distribution_channel_ids = explode(',', $distribution_channel_id);
                $orderItem = Order::getTodayData2($distribution_channel_ids);

                $item['today_year_order_num'] = $orderItem['year_order_num'];
                $item['today_normal_order_num'] = $orderItem['normal_order_num'];
                $item['today_total_order_count'] = $orderItem['total_order_count'];
                $item['today_total_charge_amount'] = $orderItem['total_charge_amount'];
                $item['today_new_register_user_num'] = User::getTodayRegisterUserCount($distribution_channel_ids);
                $item['today_new_fans_recharge_amount'] = number_format(Order::getFansToDayOrderAmount($distribution_channel_ids), 2);
                $item['today_new_fans_num'] = ForceSubscribeUsers::getSubscribleCount($distribution_channel_ids, date('Y-m-d'));
                $result[] = $item;
            }
            return response()->success($result);
        }
    }

    public function getAccountInfo(Request $request)
    {
        $channel_user_ids = $this->getChannelUsers($request);
        if (count($channel_user_ids) <= 0) {
            return response()->error('NOT_EXIST');
        } else {
            $result = [];
            foreach ($channel_user_ids as $channel_user_id) {
                $item = [];
                $channel_user = ChannelUser::getById($channel_user_id);
                $item['phone'] = $channel_user->phone;
                $distribution_channel_ids = ChannelUser::getChannelIdArray($channel_user_id);
                $orderItem = Order::getTodayData2($distribution_channel_ids);

                $item['today_year_order_num'] = $orderItem['year_order_num'];
                $item['today_normal_order_num'] = $orderItem['normal_order_num'];
                $item['today_total_order_count'] = $orderItem['total_order_count'];
                $item['today_total_charge_amount'] = $orderItem['total_charge_amount'];
                $item['today_new_register_user_num'] = User::getTodayRegisterUserCount($distribution_channel_ids);
                $item['today_new_fans_recharge_amount'] = number_format(Order::getFansToDayOrderAmount($distribution_channel_ids), 2);
                $item['today_new_fans_num'] = ForceSubscribeUsers::getSubscribleCount($distribution_channel_ids, date('Y-m-d'));
                $result[] = $item;
            }
            return response()->success($result);
        }
    }

    public function getSiteYesterdayChargeInfo(Request $request)
    {
        $distribution_channel_ids = $this->getDistributionChannelIds($request);
        if (count($distribution_channel_ids) <= 0) {
            return response()->error('NOT_EXIST');
        } else {
            $date = date('Y-m-d', strtotime('-1 day'));
            $result = OrderDayStat::getSiteYesterdayChargeInfo($distribution_channel_ids, $date);
            return response()->success($result);
        }
    }
}